package com.zmn.oms.dubbo.impl.zmn.channel.order;

import org.apache.dubbo.config.annotation.Service;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.common.utils.validator.ValidateResult;
import com.zmn.common.utils.validator.ValidatorUtil;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.dubbo.dto.zmn.order.CancelOrderDIO;
import com.zmn.oms.dubbo.dto.zmn.order.NewOrderDIO;
import com.zmn.oms.dubbo.dto.zmn.order.NewOrderDRO;
import com.zmn.oms.dubbo.dto.zmn.order.UpdateOrderDIO;
import com.zmn.oms.dubbo.impl.zmn.normal.order.ZsNormalOrderRemoteServiceImpl;
import com.zmn.oms.dubbo.impl.zmn.order.OrderBaseRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.channel.order.ZsChannelOrderRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.order.zmn.ZsNormalNewOrderDTO;
import com.zmn.oms.model.dto.order.zmn.ZsNormalUpdateOrderDTO;

/**
 * 类描述：商家渠道订单 remote service
 *
 * @author liuying
 * @date 2018/11/07 15:10
 */
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsChannelOrderRemoteServiceImpl extends OrderBaseRemoteService implements ZsChannelOrderRemoteService {

    static final String TAG = "啄木鸟-商家单";

    @Override
    public ResponseDTO cancelOrder(CancelOrderDIO cancelOrderDIO) {
        logger.info("[{}] 取消订单：{}", TAG, cancelOrderDIO);

        ValidateResult validateResult = ValidatorUtil.validator(cancelOrderDIO);
        if (!validateResult.isSuccess()) {
            logger.error("[{}] 参数错误：{}", TAG, validateResult.toString());
            return ResponseUtil.responseFail("提交参数错误");
        }

        // 商户类型
        cancelOrderDIO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_CHANNEL);

        return super.cancelOrder(cancelOrderDIO);
    }

    @Override
    public ResponseDTO<NewOrderDRO> updateOrder(UpdateOrderDIO updateOrderDIO) {
        logger.info("[{}] 修改订单：{}", TAG, updateOrderDIO);

        ValidateResult validateResult = ValidatorUtil.validator(updateOrderDIO);
        if (!validateResult.isSuccess()) {
            logger.error("[{}] 参数错误：{}", TAG, validateResult.toString());
            return ResponseUtil.responseFail("提交参数错误：" + validateResult.toString());
        }

        // 商户类型
        updateOrderDIO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_CHANNEL);

        // DIO转换
        ZsNormalUpdateOrderDTO normalNewOrderDTO = BeanMapper.map(updateOrderDIO, ZsNormalUpdateOrderDTO.class);

        return super.updateOrder(normalNewOrderDTO);
    }

    @Override
    public ResponseDTO<NewOrderDRO> addOrder(NewOrderDIO newOrderDIO) {
        logger.info("[{}] 添加订单：{}", TAG, newOrderDIO);

        ValidateResult validateResult = ValidatorUtil.validator(newOrderDIO);
        if (!validateResult.isSuccess()) {
            logger.error("[{}] 参数错误：{}", TAG, validateResult.toString());
            return ResponseUtil.responseFail("提交参数错误：" + validateResult.toString());
        }

        // 商户类型
        newOrderDIO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_CHANNEL);

        // DIO转换
        ZsNormalNewOrderDTO normalNewOrderDTO = BeanMapper.map(newOrderDIO, ZsNormalNewOrderDTO.class);
        normalNewOrderDTO.setReceiverManner(OrderConsts.RECEIVE_MANNER_MANUAL);
        logger.info("未替换下单接口：[com.zmn.oms.dubbo.impl.zmn.channel.order.ZsChannelOrderRemoteServiceImpl.addOrder]，下单渠道：[{}]，下单方式：[{}]", normalNewOrderDTO.getChannelId(), normalNewOrderDTO.getReceiveEntranceId());
        return super.addOrder(normalNewOrderDTO);
    }

}
